package com.hyphenate.chatuidemo.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hyphenate.easeui.ui.EaseChatFragment;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.MessageAccountListBean;

import com.joplus.housekeeper.ui.activity.PersonDataActivity;

import com.joplus.housekeeper.utils.SystemBarTintManager;

import java.io.File;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by zisheng on 2017/9/1.
 */

public class ChatActivityNew extends BaseActivity {

    String TAG = "ChatActivityNew";
    public static ChatActivityNew activityInstance;
    public static String userName;
    public EaseChatFragment chatFragment;
    String toChatUsername;
    private CircleImageView ivLeftView, profileImage;
    private ImageView ivRightView;


    Bundle bundle;

    RelativeLayout layout_image;

    private LinearLayout ll_parent;


    private FrameLayout container;
    private TextView tv_title;
    public MessageAccountListBean user;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.em_activity_chat_new);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        //使StatusBarTintView 和 actionbar的颜色保持一致，风格统一。
        tintManager.setStatusBarTintResource(R.color.black);
        // 设置状态栏的文字颜色
        tintManager.setStatusBarDarkMode(true, this);
        activityInstance = this;
        init();


    }

    private void init() {
        userName = getIntent().getStringExtra("userId");
        user = getIntent().getParcelableExtra("user");
        //可以直接new EaseChatFratFragment使用
        chatFragment = new ChatFragment();
        //传入参数
        chatFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();

        //聊天人或群id
        toChatUsername = user.getAccountId();


        profileImage = (CircleImageView) findViewById(R.id.profile_image);
        ivRightView = (ImageView) findViewById(R.id.iv_main_right);
        tv_title = (TextView) findViewById(R.id.tv_title);


        container = (FrameLayout) findViewById(R.id.container);


        ll_parent = (LinearLayout) findViewById(R.id.ll_parent);

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivRightView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ChatActivityNew.this, PersonDataActivity.class);

                intent.putExtra("user", user);
                startActivity(intent);


            }
        });
        tv_title.setText(user.getName());
    }
    //   tv_title.setText(xmppUser.getNickName());}

    //发消息给客户
    public void sendMessageToCustom(String message) {
        chatFragment.insertMessage(message);
    }

    //发送图片给客服
    public void sendPicToService(Uri uri) {
        chatFragment.sendPicByUri(uri);
    }


    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");
        activityInstance = null;

    }

    @Override
    protected void onNewIntent(Intent intent) {
        // 点击notification bar进入聊天页面，保证只有一个聊天页面
        //singleTask模式下在这里获取intent
        setIntent(intent);
        String username = intent.getStringExtra("userId");

        if (toChatUsername.equals(username))
            super.onNewIntent(intent);
        else {
            finish();
            startActivity(intent);
        }
    }


    private int keyDownCount = 0;
    private boolean isOut = false;


    public void insertVoice(String filePath, int length, String user) {
        chatFragment.insertVoice(filePath, length, user);
    }

    public void sendPic2Custome(File file) {

        chatFragment.insertPic(file);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}

